package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 贾育权~
 * @version 1.0
 */
@Service
public class DishServiceImp implements DishService {
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味//
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    @Transactional
    @Override
    public Result saveWithFlavor(DishDTO dishDTO) {//转换为实体dish

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表中插入语句
        // DishMapper;
        dishMapper.insert(dish);//新增菜品表同时更新我们的口味表
         //获取insert语句生成的主键值
        Long id = dish.getId();
        //向口味表中插入数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            //flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            //向口味表插入n数据
            dishFlavorMapper.insertBatch(flavors);
        }

        return null;
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //帮助我们分页的 插件，紧跟着的sql语句会自动增加limit
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

       Page <DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);


        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteByIds(List<Long> ids) {//删除菜品//根据我们传入的许多的id
        //我们要删除的多个菜品中有看销售状态，如果有正在售售的菜品就抛出异常
        for(Long id: ids){//遍历每一个id
            Dish dish = dishMapper.getById(id);//根据id查询菜品
            if(dish.getStatus()== StatusConstant.ENABLE);//在售的
            throw new RuntimeException("删除菜品中有正在售售的菜品，无法删除");
        }


        //判断当前菜品是否能够删除---是否被套餐关联了？？
          //通过调用这个方法我们就能得到套餐的id -包含我们要删除的菜品的id
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);

        if (setmealIds != null && setmealIds.size() > 0) {//
            //当前菜品被套餐关联了，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }//
       for(Long id:ids){//遍历每一个id 删除
           dishMapper.deleteById(id);//批量的删除的操作
       }

    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {//菜品的回显
        //回显菜品的基本的信息
       // DishVO dishVO = dishMapper.getByIdWithDish(id);

        //回显菜品的口味信息


        //进行两方面数据的汇总


        //根据id查询菜品数据
        Dish dish = dishMapper.getById(id);

        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        //将查询到的数据封装到VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;

    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();//将dto转换为dish
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品表
        dishMapper.update(dish);

        //删除口味表//就是我们把原有的数据删除掉 ，用新的数据进行覆盖
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //添加口味表根据我们传入的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){//口味数据对应的菜品id是空的
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            //向口味表插入n数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public void startOrStop(Integer status, Long id) {//我们是要update 操作

        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();//构建一个dish对象 如果我们传进去的值为空的话我们就是不再更新这个字段

        dishMapper.update(dish);
    }

    @Override
    public List<Dish> list(Long categoryId) {//菜品的查询同一个分类的id
       //生成一个dish 对象
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        return dishMapper.list(dish);

       //return dishMapper.selectByCategoryId(categoryId);


    }


}
